package com.haierp.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.CategoryMapper;
import com.haierp.model.Category;
import com.haierp.service.ICategoryService;
import com.wangzhixuan.commons.result.Tree;

@Service
public class CategoryServiceImpl extends SuperServiceImpl<CategoryMapper, Category>  implements ICategoryService {

	@Autowired
	private CategoryMapper categoryMapper;

	@Override
	public List<Category> selectAll() {
		EntityWrapper<Category> wrapper = new EntityWrapper<Category>();
		wrapper.orderBy("seq");
//		wrapper.where("status={0}", "0");
		return categoryMapper.selectList(wrapper);
	}

	@Override
	public List<Tree> selectAllMenu() {
		List<Tree> trees = new ArrayList<Tree>();
		// 查询所有菜单
		List<Category> categories = this.selectAll();
		if (categories == null) {
			return trees;
		}
		for (Category category : categories) {
			Tree tree = new Tree();
			tree.setId(category.getId());
			tree.setPid(category.getPid());
			tree.setText(category.getName());
//			tree.setIconCls(resource.getIcon());
//			tree.setAttributes(resource.getUrl());
			trees.add(tree);
		}
		return trees;
	}

//	@Override
//	public void insert(Category category) {
//		categoryMapper.insert(category);		
//	}

	@Override
	public Category findCategory(Long id) {
		return categoryMapper.selectById(id);
	}

	@Override
	public List<Tree> selectAllValid() {
		List<Tree> trees = new ArrayList<Tree>();
		EntityWrapper<Category> wrapper = new EntityWrapper<Category>();
		wrapper.orderBy("seq");
		wrapper.where("status={0}", "0");
		List<Category> categories =categoryMapper.selectList(wrapper);
		if (categories == null) {
			return trees;
		}
		for (Category category : categories) {
			Tree tree = new Tree();
			tree.setId(category.getId());
			tree.setPid(category.getPid());
			tree.setText(category.getName());
//			tree.setIconCls(resource.getIcon());
//			tree.setAttributes(resource.getUrl());
			trees.add(tree);
		}
		return trees;
	}

	@Override
	public List<Category> tree() {
		List<Category> cates = selectAll();
		List<Category> cateListlevel1 = new ArrayList<>();
		if(cates!=null&&!cates.isEmpty()){
			Map<Long,Category> categoryLevel1 = new HashMap<>();
			Map<Long,Category> categoryLevel2 = new HashMap<>();
			cates.forEach(cate->{
				if(cate.getLevel()==1){
					categoryLevel1.put(cate.getId(), cate);
					cateListlevel1.add(cate);
				}else if(cate.getLevel()==2){
					categoryLevel2.put(cate.getId(), cate);
				}
			});
			cates.forEach(cate->{
				if(cate.getLevel()==3){
					Category category = categoryLevel2.get(cate.getPid());
					category.getChildren().add(cate);
				} else if(cate.getLevel()==2){
					Category category = categoryLevel1.get(cate.getPid());
					category.getChildren().add(cate);
				}
			});
		}
		return cateListlevel1;
	}

//	@Override
//	public void update(Category category) {
//		categoryMapper.update(category, null);
//	}
}
